﻿using System;
using Resources;
using mojoPortal.Web;

namespace ud_GoogleMap
{
    
    public partial class GoogleMapModule : SiteModuleControl
    {
        private GoogleMapConfiguration config = new GoogleMapConfiguration();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {
            gmap.Location = config.Location;
            gmap.GMapApiKey = config.GoogleApiKey.Length > 0 ? config.GoogleApiKey : SiteUtils.GetGmapApiKey();
            gmap.EnableMapType = config.EnableMapType;
            gmap.EnableZoom = config.EnableZoom;
            gmap.ShowInfoWindow = config.ShowInfoWindow;
            gmap.EnableLocalSearch = config.EnableLocalSearch;
            gmap.MapHeight = config.MapHeight;
            gmap.MapWidth = config.MapWidth;
            gmap.EnableDrivingDirections = config.EnableDrivingDirections;
            gmap.GmapType = config.GoogleMapType;
            gmap.ZoomLevel = config.ZoomSetting;
            if (config.Caption.Length > 0) { litCaption.Text = config.Caption + @"<br />"; }
            if (config.UseLocationAsCaption) { litCaption.Text = Server.HtmlEncode(gmap.Location) + @"<br />"; }
        }


        private void PopulateLabels()
        {
            pnlContainer.ModuleId = ModuleId;
            Title1.Visible = !RenderInWebPartMode;
            if (ModuleConfiguration != null)
            {
                if (config.UseLocationAsTitle)
                {
                    ModuleConfiguration.ModuleTitle = gmap.Location;
                }

                Title = ModuleConfiguration.ModuleTitle;
                Description = ModuleConfiguration.FeatureName;
            }

            gmap.DirectionsButtonText = GMapResources.GoogleMapGetDirectionsFromButton;
            gmap.DirectionsButtonToolTip = GMapResources.GoogleMapGetDirectionsFromButtonToolTip;
            gmap.NoApiKeyWarning = GMapResources.NoApiKeyWarning;
        }

        private void LoadSettings()
        {
            config = new GoogleMapConfiguration(Settings);

            if (config.InstanceCssClass.Length > 0) { pnlContainer.CssClass += " " + config.InstanceCssClass; }

           
        }

        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;

        }

        #endregion


    }
}